/*
 * @Author: SingleBiu
 * @Date: 2025-03-30 21:12:14
 * @LastEditors: SingleBiu
 * @LastEditTime: 2025-03-30 22:10:31
 * @Description: 并行计算例子
 */
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#include<time.h>

#define N 100

int main()
{
    clock_t start1,start2,end1,end2;
    double cpu_time_used1,cpu_time_used2;

    float sum = 0.0;
    float arr[N] = {0};

    for (int i = 0; i < N; i++)
    {
        arr[i] = i;
    }

    start1 = clock();
    #pragma omp parallel for reduction(+:sum)
    for (int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
    end1 = clock();
    cpu_time_used1 = ((double)(end1-start1)) / CLOCKS_PER_SEC;
    printf("CPU used: %f sec\n",cpu_time_used1);
    printf("Sum1 = %f\n",sum);
    
    sum = 0;
    
    start2 = clock();
    for (int i = 0; i < N; i++)
    {
        sum += arr[i];
    }
    end2 = clock();
    cpu_time_used2 = ((double)(end2-start2)) / CLOCKS_PER_SEC;
    printf("CPU used: %f sec\n",cpu_time_used2);

    printf("Sum2 = %f\n",sum);


    // //使用6核进行并行输出
    // #pragma omp parallel num_threads(6)
    // {
    //     int i = omp_get_thread_num();
    //     printf("Hello from thread %d\n",i);
    // }
    
    // system("pause\n");
    return 0;
}

// 谢谢不黑我的键盘了！！！
// 非常感谢！！！
